<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1100" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-col :span="12">
                <el-form-item label="客户代码" prop="customer_code">
                    <el-input v-model="form.customer_code" placeholder="请输入客户代码" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户名称" prop="customer_name">
                        <el-input v-model="form.customer_name" placeholder="请输入客户名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="编号" prop="customer_num">
                        <el-input v-model="form.customer_num" placeholder="请输入编号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="24">
                <el-form-item label="联系人" prop="customer_detail">
                    <sc-form-table ref="table" v-model="form.customer_detail" :addTemplate="addTemplate" placeholder="暂无数据" drag-sort height="250">
                        <el-table-column prop="contacts" label="姓名" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.contacts" placeholder="请输入姓名"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="mobile" label="手机号" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.mobile" placeholder="请输入手机号"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="form.address" placeholder="请输入地址" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="来源" prop="source">
                        <el-select v-model="form.source" filterable style="width: 100%" placeholder="请选择来源">
                            <el-option v-for="item in sourceList" :key="item.id" :label="item.source_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="期初应收" prop="collect_price">
                        <el-input v-model="form.collect_price" placeholder="请输入期初应收" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="0" :max="9999" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="附件" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                </sc-upload-file>
            </el-form-item>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
                </el-form-item>
            </el-col>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: '新增客户',
        edit: '编辑客户',
        show: '查看客户'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        id: "",
        customer_code: "",
        customer_num: "",
        customer_name: "",
        address: "",
        source: "",
        collect_price: "",
        // advance_price: "",
        enclosure: "",
        remark: "",
        system_disable: "",
        customer_detail: [],
        sort: "",
      },
      //验证规则
      rules: {
        customer_code: [{required: true, message: '请输入客户代码'}],
        customer_name: [{required: true, message: '请输入客户名称'}],
        collect_price: [{required: true, message: '请输入期初应收'}],
        customer_detail: [{required: true, message: '请添加联系人'}],
      },
      addTemplate: {},
      sourceList: [],
    }
  },
  mounted() {
    this.getSource()
  },
  methods: {
    //显示
    open(mode = 'add') {
      this.mode = mode;
      this.visible = true;
      return this
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = [];
          if (this.form.id) {
            res = await this.$API.client.client.edit.post(this.form);
          } else {
            res = await this.$API.client.client.add.post(this.form);
          }
          this.isSaveing = false;
          if (res.code == 1) {
            this.$emit('success', this.form, this.mode)
            this.visible = false;
            this.$message.success("操作成功")
          } else {
            this.$alert(res.message, "提示", {type: 'error'})
          }
        } else {
          return false;
        }
      })
    },
    getPid(data) {
      this.form.type_id = data
    },
    //表单注入数据
    async setData(data) {
      this.form.id = data.id
      var res = await this.$API.client.client.detail.get({id: data.id});
      console.log(res);
      this.form.customer_code = res.data.customer_code
      this.form.customer_num = res.data.customer_num
      this.form.customer_name = res.data.customer_name
      this.form.address = res.data.address
      this.form.source = Number(res.data.source)
      this.form.collect_price = res.data.collect_price
      // this.form.advance_price = res.data.advance_price
      this.form.enclosure = res.data.enclosure
      this.form.remark = res.data.remark
      this.form.system_disable = res.data.system_disable
      let array = []
      res.data.customer.map(item => {
        array.push({
          id: item.id,
          contacts: item.contacts,
          mobile: item.mobile,
        })
      })
      this.form.customer_detail = array
    this.form.sort = data.sort
    },
    // 获取分类
    async getSource() {
      var res = await this.$API.client.source.all.get();
      this.sourceList = res.data
    },
    onFocus(event) {
      event.target.select();
    },
  }
}
</script>

<style></style>
